package com.example.lianxirenlist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class ListShuJuKu extends SQLiteOpenHelper {
    private  static final String TAG="21555";
    public ListShuJuKu(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public ListShuJuKu(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
       sqLiteDatabase.execSQL("create table user(_id integer primary key autoincrement,name text not null)");
       sqLiteDatabase.execSQL("create table phone(_id integer primary key autoincrement ,user_id intger not null,call text not null,tel text not null)");

    }
    public boolean insertUser(String name){
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        long na=getWritableDatabase().insert("user",null,cv);
        return na!=-1;
    }
    public boolean insertphone(String id,String call,String tel){
        ContentValues cb=new ContentValues();
        cb.put("user_id",id);
        cb.put("call",call);
        cb.put("tel",tel);
       long phe= getWritableDatabase().insert("phone",null,cb);
       return phe!=-1;
    }


    public void interlist(DataBean d){
        SQLiteDatabase sd=getWritableDatabase();
        try {
            sd.beginTransaction();
           boolean l=insertUser(d.getName());
           Cursor cursor=getWritableDatabase().query("user",new String[]{"_id"},"name=?",new String[]{d.getName()},null,null,null);
           cursor.moveToFirst();
            String userid=null;
               do {
                    userid=cursor.getString(cursor.getColumnIndexOrThrow("_id"));
               }while (cursor.moveToNext());
           boolean phone=insertphone(userid,d.getPhone(),d.getTel());
            if (l&&phone){
                sd.setTransactionSuccessful();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sd.endTransaction();
        }
    }
    public List<DataBean> select(){
        SQLiteDatabase sd=getWritableDatabase();
        List<DataBean> l=new ArrayList<>();
        try {
            sd.beginTransaction();
            Cursor cursor=getWritableDatabase().query("user",new String[]{"_id","name"},null,null,null,null,null);
            cursor.moveToFirst();
            if (cursor.getCount()>0){
                do {
                    DataBean data=new DataBean();
                    String username=cursor.getString(cursor.getColumnIndexOrThrow("name"));
                    String userid=cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                    data.setName(username);
                    data.setId(userid);
                    l.add(data);
                }while (cursor.moveToNext());
                sd.setTransactionSuccessful();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sd.endTransaction();
        }
        return  l;
    }

    public String  selectUser(String id){
        Cursor cursor=getWritableDatabase().query("user",new String[]{"name"},"_id=?",new String[]{id},null,null,null);
        cursor.moveToFirst();
        String name="";
        if (cursor.getCount()>0){
            do {
                name=cursor.getString(cursor.getColumnIndexOrThrow("name"));
            }while (cursor.moveToNext());


        }
        cursor.close();
        return name;
    }
    public DataBean selectphone(String id){
        Cursor cursor=getWritableDatabase().query("phone",new String[]{"call","tel"},"user_id=?",new String[]{id},null,null,null,null);
        cursor.moveToFirst();
        DataBean d=new DataBean();
        if (cursor.getCount()>0){
            do {
                String call=cursor.getString(cursor.getColumnIndexOrThrow("call"));
                String tel=cursor.getString(cursor.getColumnIndexOrThrow("tel"));
                d.setPhone(call);
                d.setTel(tel);
            }while (cursor.moveToNext());
            return d;

        }
        return null;
    }
    public  boolean deleteuser(String id){
        int count=getWritableDatabase().delete("user","_id=?",new String[]{id});
        return  count>0;
    }
    public boolean deletephone(String id){
        int count=getWritableDatabase().delete("phone","user_id=?",new String[]{id});
        return count>0;
    }
    public boolean updateuser(String id,String name){
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        int count=getWritableDatabase().update("user",cv,"_id=?",new String[]{id});
        return count>0;
    }
    public boolean updatephone(String id,String call,String tel){
        ContentValues cb=new ContentValues();
        cb.put("call",call);
        cb.put("tel",tel);
        int cou=getWritableDatabase().update("phone",cb,"user_id=?",new String[]{id});
        return cou>0;
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
